//
//  YFLogisticsViewController.m
//  QuanJieShopProject
//
//  Created by apple on 2018/5/28.
//  Copyright © 2018年 apple. All rights reserved.
//
#import "YFLogisticsViewController.h"

//#import "YFCityPartnerCell.h"
//#import "YFCityPartnerHeaderView.h"

#import "YFLogisticsHeaderView.h"
#import "YFLogisticsCell.h"

@interface YFLogisticsViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, copy) NSString *orderNum;


@end

@implementation YFLogisticsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.automaticallyAdjustsScrollViewInsets = NO;
    self.title = @"物流信息";
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self requestLogisticsNum];
    
    [self setupUI];
}

- (void)requestLogisticsNum{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:@"1831" forKey:@"order_id"]; // self.orderID

    // 获取物流订单号
    [WWNetRequest fetchLogisticsNumWithParameters:dict success:^(id response) {
        NSLog(@"物流订单号 response=%@", response);
        
        if (response) {
            
            NSArray *dataArr = response[@"data"];
            NSDictionary *dataDict = dataArr.firstObject;
            
            NSMutableDictionary *infoDict = [NSMutableDictionary dictionary];
            [infoDict setObject:dataDict[@"id"] forKey:@"id"];
            [infoDict setObject:dataDict[@"invoice_no"] forKey:@"no"];
            
            [self requestLogisticsInfo:infoDict];
        }
    } failure:^(NSDictionary *error) {
        NSLog(@"物流订单号 error=%@", error);
    }];
}

- (void)requestLogisticsInfo:(NSDictionary *)dict {
    
    // 获取物流订信息 http://www.quanjieshop.com/index.php/Api/Appother/send_shipping?id=49&no=632729972129
    NSLog(@"物流订信息dict = %@", dict);
    [WWNetRequest fetchLogisticsInfoWithParameters:dict success:^(id response) {
        NSLog(@"物流订信息 response=%@", response);
        
        NSMutableArray *tempArr = [NSMutableArray array];
        NSArray *resultArr = response[@"result"][@"list"];
        self.orderNum = [response[@"result"]objectForKey:@"number"];
        for (NSDictionary *dict in resultArr) {
            YFLogisticsModel *model = [[YFLogisticsModel alloc] initWithDictionary:dict error:nil];
            [tempArr addObject:model];
        }
        self.dataArr = tempArr;
        
        [self.tableView reloadData];
        
    } failure:^(NSDictionary *error) {
        NSLog(@"物流订信息 error=%@", error);
    }];
}

#pragma mark - delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YFLogisticsCell *cell = [YFLogisticsCell cellWithTableView:tableView];
    cell.model = self.dataArr[indexPath.row];
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 135;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    YFLogisticsHeaderView *view = (YFLogisticsHeaderView *)[[[NSBundle mainBundle] loadNibNamed:@"YFLogisticsHeaderView" owner:self options:nil] lastObject];
   
    view.orderNum.text = [NSString stringWithFormat:@"订单号:%@",self.orderNum];
    return view;
}


- (void)setupUI {
    self.view.backgroundColor = YFColor_Background;
    CGFloat top = IOS_VERSION < 11.0 ? Height_NavBar : 0;
    
    // 红色背景框
    UIView *topBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH, 250.f/1334.f * UISCREEN_HEIGHT)];
    topBackgroundView.backgroundColor = YFColor_Red;
    [self.view addSubview:topBackgroundView];
    
    // 顶部图片  750 100
    CGFloat imgWH = UISCREEN_WIDTH;
    UIImageView *imgView = [[UIImageView alloc] init];
    imgView.image = [UIImage imageNamed:@"LogisticsTopView"];
    imgView.frame = CGRectMake(0, topBackgroundView.mj_y, imgWH, 100.f/750.f * imgWH);
    imgView.backgroundColor = YFColor_Red;
    [self.view addSubview:imgView];
    
    // tableVIew
    CGFloat padding = 30.f/750.f * UISCREEN_WIDTH;
    CGFloat tableViewH = 930.f/1334.f * UISCREEN_HEIGHT;
    CGFloat tableViewW = UISCREEN_WIDTH - padding * 2;
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(padding, CGRectGetMaxY(imgView.frame), tableViewW, tableViewH)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//    _tableView.contentInset = UIEdgeInsetsMake(0, 0, 60, 0);
    _tableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_tableView];
    
    

    
}

ARRAY_INIT(dataArr);

@end
